var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        form:{},
        fenleilist:[],
        productlist:[],
       keywords:'',
       showsearch:false,
       activeStatus:0,
       showAll:true, 
       showMenu:true,
    },
    methods: {
        get_data:function(){
            var that=this;
            var option={};
            option.ID=that.ID;
            option.action='getcouponinfo';
            ns.post(option, function(succeed, data, err) {
                if(succeed){
                    if(data.status){
                        that.form=data.data;
                        if(data.data.AmountType==4){
                            that.fenleilist=data.data.AppointList;

                        }else if(data.data.AmountType==5){
                            that.productlist=data.data.AppointList;
                        }
                    }
                   
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });

        },
        //编辑
        do_editorder:function(){
            var that=this;
            ns.openWin('youhuiCard_add', '编辑卡券',{
                id:that.ID
            });
        },
        do_removecard:function(){
            var that=this;
            var option={};
            option.CouponID=that.ID;
            option.action='delmallcouponcode';
            ns.post(option, function(succeed, data, err) {
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: '删除成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name:'load_card'
                        });
                        setTimeout(() => {
                            api.closeWin();
                        }, 500);
                    }else{
                        api.toast({
                            msg: '删除失败',
                            duration: 2000,
                            location: 'bottom'
                        }); 
                    }
                   
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //发布卡券
        do_sendcard:function(){
            var that=this;
            that.$dialog.confirm({
                title: '发布提示',
                message: '确定要发放优惠券吗？',
                confirmButtonText:'确认',
                cancelButtonText:'取消',
            }).then(() => {
                const idlist=[];
                idlist.push(that.ID);
                var option={};
                option.IDList=JSON.stringify(idlist);
                option.action='sendmallusercouponcode';
                ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        if(data.status){
                            api.toast({
                                msg: '发布成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name:'load_card'
                            });
                        }
                       
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
    
                });
            }).catch(() => {
                // on cancel
              });
        },
        //卡券失效
        do_shixiao:function(){
            var that=this;
            that.$dialog.confirm({
                title: '失效提示',
                message: '用户已领取，未使用的优惠券都会失效。是否确认要进行失效操作？',
                confirmButtonText:'确认',
                cancelButtonText:'取消',
            }).then(() => {
                const idlist=[];
                idlist.push(that.ID);
                var option={};
                option.IDList=JSON.stringify(idlist);
                option.action='stopmallcouponcode';
                ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        if(data.status){
                            api.toast({
                                msg: '成功失效',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name:'load_card'
                            });
                        }
                       
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
    
                });
            }).catch(() => {
                // on cancel
              });
        },
         //展开全部
         do_openall:function(){
            var that=this;
            that.showAll=!that.showAll;
        },
        do_opentype:function(){
            var that=this;
            that.showMenu=!that.showMenu;
        },
    }
       


      
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    api.addEventListener({
        name: 'do_open_saixuan'
    }, function(ret, err) {
        app.showsearch=true;
    });
    app.ID = api.pageParam.id||0;
    app.get_data();
    api.addEventListener({
        name: 'load_card'
    }, function(ret, err) {
        app.get_data();
    });

};
